package com.ph.irp.base.assistantmaterial.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.ph.irp.base.assistantmaterial.service.PBaseAssistantmaterialDeliverymethodService;
import com.ph.irp.model.PBaseAssistantmaterialDeliverymethod;

public class AssistantmaterialDeliverymethodAction extends ActionSupport {
	private PBaseAssistantmaterialDeliverymethodService deliveryMethodService;

	public void setDeliveryMethodService(
			PBaseAssistantmaterialDeliverymethodService deliveryMethodService) {
		this.deliveryMethodService = deliveryMethodService;
	}

	private List<PBaseAssistantmaterialDeliverymethod> deliveryMethods;
	private PBaseAssistantmaterialDeliverymethod deliveryMethod;
	private Object[] rowlist = null;

	public List<PBaseAssistantmaterialDeliverymethod> getDeliveryMethods() {
		return deliveryMethods;
	}

	public void setDeliveryMethods(
			List<PBaseAssistantmaterialDeliverymethod> deliveryMethods) {
		this.deliveryMethods = deliveryMethods;
	}

	public PBaseAssistantmaterialDeliverymethod getDeliveryMethod() {
		return deliveryMethod;
	}

	public void setDeliveryMethod(
			PBaseAssistantmaterialDeliverymethod deliveryMethod) {
		this.deliveryMethod = deliveryMethod;
	}

	public Object[] getRowlist() {
		return rowlist;
	}

	public void setRowlist(Object[] rowlist) {
		this.rowlist = rowlist;
	}

	public String showDeliverymethodsList() throws Throwable {
		deliveryMethod = null;
		deliveryMethods = deliveryMethodService.getDeliverymethodList();
		return "success";
	}

	@SuppressWarnings("unchecked")
	public String searchMethod() throws Throwable {
		if (deliveryMethod == null) {
			deliveryMethods = deliveryMethodService.getDeliverymethodList();
		} else {
			if (!deliveryMethod.getDeliveryMethodCode().equals("")
					&& !deliveryMethod.getDeliveryMethodName().equals("")) {
				deliveryMethods = deliveryMethodService
						.getDeliverymethodByCodeAndName(deliveryMethod
								.getDeliveryMethodCode(), deliveryMethod
								.getDeliveryMethodName());
			} else if (!deliveryMethod.getDeliveryMethodCode().equals("")) {
				deliveryMethods = deliveryMethodService
						.getDeliverymethodByCode(deliveryMethod
								.getDeliveryMethodCode());
			} else if (!deliveryMethod.getDeliveryMethodName().equals("")) {
				deliveryMethods = deliveryMethodService
						.getDeliverymethodByName(deliveryMethod
								.getDeliveryMethodName());
			} else {
				deliveryMethods = deliveryMethodService.getDeliverymethodList();
			}
		}
		deliveryMethod = null;
		return "success";
	}

	// List页面跳转到add新增页面
	public String addInputMethod() {
		deliveryMethod = null;
		return "success";
	}

	// List页面跳转到update修改页面
	public String updateInputMethod() throws Throwable {
		Integer deliveryMethodId = Integer.valueOf((String) rowlist[0]);
		deliveryMethod = deliveryMethodService
				.getDeliverymethodId(deliveryMethodId);
		return "success";
	}

	public String addMethod() throws Throwable {
		deliveryMethodService.addDeliverymethod(deliveryMethod);
		deliveryMethod = null;
		return "success";
	}

	public String updateMethod() throws Throwable {
		deliveryMethodService.updateDeliverymethod(deliveryMethod);
		deliveryMethod = null;
		return "success";
	}

	public String deleteMethod() throws Throwable {
		for (int i = 0; i < rowlist.length; i++) {
			deliveryMethodService.deleteDeliverymethod(Integer
					.valueOf((String) rowlist[i]));
		}
		return "success";
	}

	@SuppressWarnings("unchecked")
	public String selectMethod() throws Throwable {
		if (deliveryMethod == null) {
			deliveryMethods = deliveryMethodService.getDeliverymethodList();
		} else {
			if (!deliveryMethod.getDeliveryMethodCode().equals("")
					&& !deliveryMethod.getDeliveryMethodName().equals("")) {
				deliveryMethods = deliveryMethodService
						.getDeliverymethodByCodeAndName(deliveryMethod
								.getDeliveryMethodCode(), deliveryMethod
								.getDeliveryMethodName());
			} else if (!deliveryMethod.getDeliveryMethodCode().equals("")) {
				deliveryMethods = deliveryMethodService
						.getDeliverymethodByCode(deliveryMethod
								.getDeliveryMethodCode());
			} else if (!deliveryMethod.getDeliveryMethodName().equals("")) {
				deliveryMethods = deliveryMethodService
						.getDeliverymethodByName(deliveryMethod
								.getDeliveryMethodName());
			} else {
				deliveryMethods = deliveryMethodService.getDeliverymethodList();
			}
		}
		return "success";
	}

}
